package com.robot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Mock API Controller for testing purposes
 * Provides mock data for fire alarm counts and maintenance status
 */
@RestController
public class MockAPIController {

    private static final Logger logger = LoggerFactory.getLogger(MockAPIController.class);
    private static final Random random = new Random();
    
    /**
     * 获取火警次数
     */
    @GetMapping("/fire-alarm/count")
    public Map<String, Object> getFireAlarmCount(
            @RequestParam String company,
            @RequestParam Integer year,
            @RequestParam(required = false) Integer month) {
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            int count;
            if (month != null) {
                // 生成随机火警次数 (0-8)
                count = random.nextInt(9);
            } else {
                // 全年数据，生成较大的随机数
                count = random.nextInt(30) + 10;
            }
            
            String period = month != null ? year + "年" + month + "月" : year + "年";
            
            response.put("success", true);
            response.put("company", company);
            response.put("period", period);
            response.put("count", count);
            response.put("timestamp", LocalDate.now().format(DateTimeFormatter.ISO_DATE));
            
            logger.info("返回{}{}的火警次数: {}", company, period, count);
        } catch (Exception e) {
            logger.error("获取火警次数失败", e);
            response.put("success", false);
            response.put("message", "获取火警次数失败: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 获取维保状态
     */
    @GetMapping("/maintenance/status")
    public Map<String, Object> getMaintenanceStatus(
            @RequestParam String company,
            @RequestParam Integer year,
            @RequestParam(required = false) Integer month) {
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 随机维保完成率 (60%-100%)
            int completionRate = random.nextInt(41) + 60;
            
            // 随机设备总数 (根据公司规模不同)
            int totalDevices;
            switch (company) {
                case "一分公司":
                    totalDevices = random.nextInt(30) + 50; // 50-79
                    break;
                case "二分公司":
                    totalDevices = random.nextInt(20) + 30; // 30-49
                    break;
                case "三分公司":
                    totalDevices = random.nextInt(20) + 20; // 20-39
                    break;
                default:
                    totalDevices = random.nextInt(30) + 40; // 40-69
            }
            
            // 随机故障设备数 (0-10%)
            int faultyDevices = totalDevices * random.nextInt(11) / 100;
            
            String period = month != null ? year + "年" + month + "月" : year + "年";
            String status = completionRate >= 90 ? "优" : completionRate >= 75 ? "良" : "需改进";
            
            response.put("success", true);
            response.put("company", company);
            response.put("period", period);
            response.put("status", status);
            response.put("completion_rate", completionRate);
            response.put("total_devices", totalDevices);
            response.put("maintained_devices", totalDevices * completionRate / 100);
            response.put("faulty_devices", faultyDevices);
            response.put("timestamp", LocalDate.now().format(DateTimeFormatter.ISO_DATE));
            
            logger.info("返回{}{}的维保状态: 完成率{}%, 状态{}", company, period, completionRate, status);
        } catch (Exception e) {
            logger.error("获取维保状态失败", e);
            response.put("success", false);
            response.put("message", "获取维保状态失败: " + e.getMessage());
        }
        
        return response;
    }
} 